﻿using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;
using YogaStudio.Data;
using YogaStudio.Data.Domain;
using YogaStudio.Service;
using YogaStudio.Service.BLL;

namespace YogaStudio.WebUI.Controllers
{
    public class FacultiesController : Controller
    {

        // private YogaStudioEntities db = new YogaStudioEntities();

        FacultySerivce facultySerive = new FacultySerivce();

        // GET: Faculties
        public ActionResult Index()
        {
            return View(facultySerive.GetAllFaculties());
        }

        // GET: Faculties/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Faculty faculty = facultySerive.GetFaculty((int)id);
            if (faculty == null)
            {
                return HttpNotFound();
            }
            return View(faculty);
        }

        // GET: Faculties/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Faculties/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Title,FullName,PhoneNumber,Email,Address")] Faculty faculty)
        {
            if (ModelState.IsValid)
            {
                facultySerive.SaveFaculty(faculty);
                return RedirectToAction("Index");
            }

            return View(faculty);
        }

        // GET: Faculties/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Faculty faculty = facultySerive.GetFaculty((int)id);
            if (faculty == null)
            {
                return HttpNotFound();
            }
            return View(faculty);
        }

        // POST: Faculties/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Title,FullName,PhoneNumber,Email,Address")] Faculty faculty)
        {
            if (ModelState.IsValid)
            {
                facultySerive.UpdateFaculty(faculty);
                return RedirectToAction("Index");
            }
            return View(faculty);
        }

        // GET: Faculties/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Faculty faculty = facultySerive.GetFaculty((int)id);
            if (faculty == null)
            {
                return HttpNotFound();
            }
            return View(faculty);
        }

        // POST: Faculties/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Faculty faculty = facultySerive.GetFaculty((int)id);
            facultySerive.UpdateFaculty(faculty);
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                facultySerive.Dispose(true);
            }
            base.Dispose(disposing);
        }

        public ActionResult CreateAccount(int id)
        {
            Faculty faculty = (new FacultySerivce().GetFaculty(id));
            if (faculty != null)
            {
                if (faculty.UserId != null)
                {
                    TempData["AccountAreadyExist"] = true;
                    
                    return RedirectToAction("Index","Faculties");
                }
                return RedirectToAction("RegisterFaculty", "Account", new { id });
            }
            return Redirect("Index");
        }

        [HttpPost]
        public ActionResult CreateAccount()
        {
            return null;
        }
    }
}
